package com.sinothk.fc.storeMaterial.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.base.serialNumber.manager.SerialNumberHelper;
import com.sinothk.fc.storeMaterial.entity.StoreMaterialClassifyVO;
import com.sinothk.fc.storeMaterial.entity.StoreMaterialEntity;
import com.sinothk.fc.storeMaterial.entity.StoreMaterialSearchReq;
import com.sinothk.fc.storeMaterial.mapper.StoreMaterialMapper;
import com.sinothk.fc.storeMaterial.service.IStoreMaterialService;
import com.sinothk.fc.storeMaterialCost.entity.StoreMaterialCostEntity;
import com.sinothk.fc.storeMaterialCost.mapper.StoreMaterialCostMapper;
import com.sinothk.fc.storeMaterialRecord.entity.StoreMaterialRecordEntity;
import com.sinothk.fc.storeMaterialRecord.service.IStoreMaterialRecordService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @Description: 商品物料表
 * @Author: Liangyt
 * @Date: 2022-09-23
 * @Version: V1.0
 */
@Service
public class StoreMaterialServiceImpl extends ServiceImpl<StoreMaterialMapper, StoreMaterialEntity> implements IStoreMaterialService {
    @Resource
    private StoreMaterialCostMapper storeMaterialCostMapper;
    @Resource
    private StoreMaterialMapper goodsMaterialMapper;
    private final IStoreMaterialRecordService storeMaterialRecordService;

    public StoreMaterialServiceImpl(IStoreMaterialRecordService storeMaterialRecordService) {
        this.storeMaterialRecordService = storeMaterialRecordService;
    }

    @Override
    public IPage<StoreMaterialClassifyVO> listForClassify(Page<StoreMaterialEntity> page, StoreMaterialSearchReq searchReq) throws Exception {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
        searchReq.setStoreId(loginUser.getStoreId());
        return goodsMaterialMapper.listForClassify(page, searchReq);
    }

    @Override
    public IPage<StoreMaterialEntity> listForBarCode(Page<StoreMaterialEntity> page, StoreMaterialSearchReq searchReq) throws Exception {
        if (searchReq == null || StringUtils.isBlank(searchReq.getBarcode())) throw new Exception("商品条码必传");

        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (loginUser == null || StringUtils.isBlank(loginUser.getStoreId())) throw new Exception("当前用户没有门店id");
        searchReq.setStoreId(loginUser.getStoreId());

        LambdaQueryWrapper<StoreMaterialEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreMaterialEntity::getStoreId, loginUser.getStoreId())
                .eq(StoreMaterialEntity::getGoodsNo, searchReq.getBarcode())
                .orderByAsc(StoreMaterialEntity::getCpBzq);
        return goodsMaterialMapper.selectPage(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StoreMaterialRecordEntity addStockNum(StoreMaterialEntity beanReq) throws Exception {
        // 首先更新库存
        beanReq.setStockNum(beanReq.getStockNum() + beanReq.getStockInputNum());
        this.updateById(beanReq);

        // 新增物料记录
        StoreMaterialRecordEntity materialRecord = new StoreMaterialRecordEntity();
        materialRecord.setStockId(beanReq.getId());
        materialRecord.setGoodsNo(beanReq.getGoodsNo());
        materialRecord.setGoodsInPrice(beanReq.getGoodsInPrice());
        materialRecord.setStockNum(beanReq.getStockInputNum());
        materialRecord.setGoodsAllPrice(beanReq.getGoodsInPrice() * beanReq.getStockInputNum());
        materialRecord.setState(1);// 状态：0未支付1已支付2不需支付
        materialRecord.setRemark("物料同款补货");

        materialRecord.setGoodsName(beanReq.getGoodsName());
        materialRecord.setGoodsCover(beanReq.getGoodsCover());
        materialRecord.setSellerId(beanReq.getSellerId());
        materialRecord.setSellerName(beanReq.getSellerName());
        materialRecord.setSellerLogo(beanReq.getSellerLogo());
        materialRecord.setSellerPhone(beanReq.getSellerPhone());
        storeMaterialRecordService.save(materialRecord);

        return materialRecord;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StoreMaterialRecordEntity add(StoreMaterialEntity beanReq) throws Exception {
        this.save(beanReq);

        // 新增物料记录
        StoreMaterialRecordEntity materialRecord = new StoreMaterialRecordEntity();
        materialRecord.setGoodsNo(beanReq.getGoodsNo());
        materialRecord.setGoodsInPrice(beanReq.getGoodsInPrice());
        materialRecord.setStockNum(beanReq.getStockNum());
        materialRecord.setGoodsAllPrice(beanReq.getGoodsInPrice() * beanReq.getStockNum());
        materialRecord.setState(1);// 状态：0未支付1已支付2不需支付
        materialRecord.setRemark("新进物料");

        materialRecord.setGoodsName(beanReq.getGoodsName());
        materialRecord.setGoodsCover(beanReq.getGoodsCover());
        materialRecord.setSellerId(beanReq.getSellerId());
        materialRecord.setSellerName(beanReq.getSellerName());
        materialRecord.setSellerLogo(beanReq.getSellerLogo());
        materialRecord.setSellerPhone(beanReq.getSellerPhone());
        storeMaterialRecordService.save(materialRecord);
        return materialRecord;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ArrayList<StoreMaterialRecordEntity> addBatch(ArrayList<StoreMaterialEntity> materialList) {
        // 新增物料
        ArrayList<StoreMaterialRecordEntity> recordList = new ArrayList<>();
        for (StoreMaterialEntity material : materialList) {
            // 设置识别号
            if (StringUtils.isBlank(material.getGoodsNo())) {
                material.setGoodsNo(SerialNumberHelper.getSerialNumber("产品"));
            }

            // 设置记录
            StoreMaterialRecordEntity materialRecord = new StoreMaterialRecordEntity();
            materialRecord.setGoodsNo(material.getGoodsNo());
            materialRecord.setGoodsInPrice(material.getGoodsInPrice());
            materialRecord.setStockNum(material.getStockNum());
            materialRecord.setGoodsAllPrice(material.getGoodsInPrice() * material.getStockNum());
            materialRecord.setState(1);// 状态：0未支付1已支付2不需支付
            materialRecord.setRemark("新进物料");

            materialRecord.setGoodsName(material.getGoodsName());
            materialRecord.setGoodsCover(material.getGoodsCover());
            materialRecord.setSellerId(material.getSellerId());
            materialRecord.setSellerName(material.getSellerName());
            materialRecord.setSellerLogo(material.getSellerLogo());
            materialRecord.setSellerPhone(material.getSellerPhone());
            recordList.add(materialRecord);
        }
        this.saveBatch(materialList);

        // 新增入料记录
        storeMaterialRecordService.saveBatch(recordList);
        return recordList;
    }

    @Override
    public void goodsMaterialUsed(String materialId, int goodsNum) throws Exception {
        StoreMaterialEntity goodsMaterial = this.getById(materialId);
        if (goodsMaterial == null) {
            throw new Exception("物料不存在");
        }
        // 减库存
        goodsMaterial.setStockNum(goodsMaterial.getStockNum() - goodsNum);
        this.updateById(goodsMaterial);

        // 加记录
        StoreMaterialCostEntity materialCost = new StoreMaterialCostEntity();
        materialCost.setStorehouseId("");
        materialCost.setStockId(materialId);
        materialCost.setGoodsNo(goodsMaterial.getGoodsNo());
        materialCost.setGoodsName(goodsMaterial.getGoodsName());
        materialCost.setCostNum(goodsNum);
        storeMaterialCostMapper.insert(materialCost);
    }
}
